package com.example.designpatterns.behavioral.chain;

import java.util.HashMap;
import java.util.Map;

/**
 * 模拟服务端
 *
 * @author scott
 */
public class Server {

    private final Map<String, String> users = new HashMap<>();
    private Middleware middleware;

    /**
     * 设置处理者
     */
    public void setMiddleware(Middleware middleware) {
        this.middleware = middleware;
    }

    /**
     * 模拟登录接口
     */
    public boolean logIn(String email, String password) {
        if (middleware.check(email, password)) {
            System.out.println("登录认证成功。");

            // Do something useful here for authorized users.

            return true;
        }
        return false;
    }

    /**
     * 模拟注册接口
     */
    public void register(String email, String password) {
        users.put(email, password);
    }

    public boolean hasEmail(String email) {
        return users.containsKey(email);
    }

    public boolean isValidPassword(String email, String password) {
        return users.get(email).equals(password);
    }
}
